package DT8;

import javagprssrv.StructClass.structTCPServerSocket;
import javagprssrv.Common.DateTime;
import java.sql.ResultSet;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.StructClass.structIATask;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structTerm;
import javagprssrv.StructClass.structRecvData;

public class TrdTCPDataTrans_DT8 extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTerm term;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String AsynTelNo;
    java.lang.String TaskID;
    java.lang.String ZB;
    java.lang.String TelNO;
    java.lang.String YBH;
    java.lang.String WYM;
    java.lang.String WYM_17;
    java.lang.String TermNO;
    private java.lang.String TCP_TermType;
    
    public TrdTCPDataTrans_DT8(final javagprssrv.StructClass.structRecvData _recvData) {
        super("TrdTCPDataTrans_DT8");
        this.term = new javagprssrv.StructClass.structTerm();
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.TCP_TermType = "8";
        this.recvData = _recvData;
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
    }
    
    @java.lang.Override
    public void run() {
    	try
        {
          Thread.sleep(500L);
          




          int Data_0 = this._Data.charAt(0);
          if ((this._Data.charAt(0) == '\001') && (this._Data.charAt(1) == '\004')) {
            do_denglu();
          }
          if (this._Data.charAt(0) == '\003')
          {
            if (this._Data.charAt(25) == '\001')
            {
              do_xintiao();
              return;
            }
            if (this._Data.charAt(25) == '\003') {
              do_confirm();
            }
            if (this._Data.charAt(25) == '\006') {
              return;
            }
            if (this._Data.charAt(25) == '\024') {
              return;
            }
          }
          if (this._Data.charAt(0) == 'Y') {
            do_59();
          }
          if (this._Data.charAt(0) == '\003')
          {
            locateTask();
            boolean isJSSJ = false;
            try
            {
              isJSSJ = this.task.Task_isJSSJ;
            }
            catch (Exception e) {}
            if (isJSSJ) {
              synchronized (JGS_Param.IATasks)
              {
                for (int idx = JGS_Param.IATasks.size() - 1; idx >= 0; idx--) {
                  if ((((structIATask)JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.task.JSSJ_ClientIP)) && 
                    (((structIATask)JGS_Param.IATasks.elementAt(idx)).ClientPort == this.task.JSSJ_ClientPort))
                  {
                    ((structIATask)JGS_Param.IATasks.elementAt(idx)).RecvData = new DT8_Util().DT8Data2DT3Data(this._Data);
                    break;
                  }
                }
              }
            } else {
              synchronized (JGS_Param.UnSavedRecvData_TCP)
              {
                this.recvData.RecvDataType = Integer.valueOf(this.TCP_TermType).intValue();
                JGS_Param.UnSavedRecvData_TCP.add(this.recvData);
              }
            }
            int cmd = this._Data.substring(4, 5).charAt(0);
            if (cmd == 243) {
              return;
            }
            if (cmd == 244) {
              return;
            }
            if (cmd == 245) {
              return;
            }
            do_41();
          }
        }
        catch (Exception e) {}
    }
    
    private java.lang.String getWYM() {
        java.lang.String res = "";
        if (this._Data.length() >= 17) {
            final int offset = 25;
            final int rightOffset = 11;
            res = this._Data.substring(offset, offset + 17);
        }
        return res;
    }
    
    private void do_confirm() {
        final java.lang.String wym = this._Data.substring(8, 25);
        final int zlx = this._Data.charAt(25);
        final int bwlx = this._Data.charAt(26);
        final int zxlh = this._Data.charAt(27);
        final int jcdh = this._Data.charAt(28);
        final int fszt = 255;
        final java.lang.String response = this.MakeCommand(wym, 4, bwlx, zxlh, jcdh, fszt, "");
        this.TCPDataSend(this._RemoteIP, this._RemotePort, response);
    }
    
    private void do_xintiao() {
        final int idx_A55A = this._Data.indexOf("¥Z");
        final int idx_9680 = this._Data.indexOf("\u0096\u0080");
        java.lang.String crcString = this._Data.substring(idx_A55A + 2, idx_9680 - 2);
        crcString = new DT8.DT8_Util().DT8_CRC(crcString);
        final int zlx = this._Data.charAt(25);
        final int bwlx = this._Data.charAt(26);
        final int zxlh = this._Data.charAt(27);
        final int jcdh = this._Data.charAt(28);
        final java.lang.String YBID18 = this._Data.substring(7, 25);
        final java.lang.String YBID19 = this._Data.substring(8, 25);
        this.do_TermLogin(YBID19);
        final java.lang.String SJM = new DT8.DT8_Util().DT8_GetSJM();
        java.lang.String response = "";
        response = "\u0005" + YBID18 + '\u0002' + (char)bwlx + (char)zxlh + 'ÿ' + SJM;
        final java.lang.String CRC = new DT8.DT8_Util().DT8_CRC(response);
        response = "\u0003\u0000\u0000\u0000¥Z" + response + CRC + '\u0096';
        response += new DT8.DT8_Util().DT8_FillCommand(response);
        final int len = response.length();
        response = response.substring(0, 2) + (char)(len / 255) + (char)(len % 255) + response.substring(4);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, response);
    }
    //数据库不符
    private void do_TermLogin(final java.lang.String _WYM) {
        this.WYM = _WYM;
        int idx = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TCP_Terms.size(); ++i) {
            if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemotePort == this._RemotePort && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type.equals(this.TCP_TermType)) {
                idx = i;
                break;
            }
        }
        if (idx <= -1) {
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final java.lang.String _wym11 = this.WYM.substring(this.WYM.length() - 11);
            final java.lang.String sql = "select * from t_yb yb where 唯一码='" + _wym11 + "'";
            final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            try {
                rs.next();
                this.YBH = rs.getString("仪表号").trim();
                this.TelNO = rs.getString("手机卡号").trim();
            }
            catch (java.lang.Exception e) {
                javagprssrv.JGS_Param.myLog.ShowLog("[ 系统未登记该仪表 ] = " + sql, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                return;
            }
            this.term.Term_Type = this.TCP_TermType;
            this.term.Term_SerialNo = this.YBH;
            this.term.Term_TelNo = this.TelNO;
            this.term.Term_WYM = this.WYM;
            this.term.Term_RemoteIP = this._RemoteIP;
            this.term.Term_RemotePort = this._RemotePort;
            synchronized (javagprssrv.JGS_Param.TCP_Terms) {
                for (int j = javagprssrv.JGS_Param.TCP_Terms.size() - 1; j >= 0; --j) {
                    if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemotePort == this._RemotePort) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                    else if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_WYM.equals(this.WYM)) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                }
                javagprssrv.JGS_Param.TCP_Terms.add(this.term);
            }
        }
    }
    
    private void do_denglu() {
        int zxh = this._Data.charAt(7) + '\u0001';
        zxh %= 255;
        final java.lang.String res = "\u0001\u0005\u0000\u0016\u0000" + (char)zxh + '\0' + '\u0001' + '\u0002' + '\u0003' + '\u0004' + '\u0005' + '\u0006' + '\u0007' + '\b' + '\t' + '\n' + '\u000b' + '\f' + '\r' + '\u000e' + '\u000f';
        this.TCPDataSend(this._RemoteIP, this._RemotePort, res);
    }
    
    private void locateTask() {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    break;
                }
            }
        }
    }
    
    private void do_41() throws java.lang.Exception {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    if (this.JobIndex >= javagprssrv.JGS_Param.Tasks.elementAt(i).Task_Jobs.size()) {
                        javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = "2000-01-01 01:02:03";
                        break;
                    }
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_59() throws java.lang.Exception {
        this.AsynTelNo = this._Data.substring(1, 12);
        int idxTask = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 1) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).TaskAsynTelNO.equals(this.AsynTelNo)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.WYM_17 = this.task.TaskAsynTelNO_ALL;
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP = this.recvData.RemoteIP;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port = java.lang.String.valueOf(this.recvData.RemotePort);
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State = 2;
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_nextJob() throws java.lang.Exception {
        java.lang.String job = this.task.Task_Jobs.elementAt(this.JobIndex).trim().toUpperCase();
        job += "                ";
        if (job.substring(0, 9).equals("GET DATE ")) {
            this.do_getDate(job.trim());
        }
        if (job.substring(0, 10).equals("GET MONTH ")) {
            this.do_getMonth(job.trim());
        }
        if (job.substring(0, 9).equals("SET DATE ")) {
            this.do_setDate(job.trim());
        }
        if (job.substring(0, 10).equals("SET MONTH ")) {
            this.do_setMonth(job.trim());
        }
    }
    
    private java.lang.String MakeCommand(final java.lang.String _WYM_17, final int _ZLX, final int _BWLX, final int _ZXLH, final int _JCDH, final int _QXSJLX, final java.lang.String _CMD) {
        java.lang.String res = "";
        res = "\u0000" + _WYM_17 + (char)_ZLX + (char)_BWLX + (char)_ZXLH + (char)_JCDH + (char)_QXSJLX + _CMD;
        final int cmdLength = 2 + _CMD.length();
        res = "" + (char)cmdLength + res;
        final java.lang.String CRC = new DT8.DT8_Util().DT8_CRC(res);
        res = "\u0003\u0000\u0000\u0000¥Z" + res + CRC + '\u0096';
        res += new DT8.DT8_Util().DT8_FillCommand(res);
        final int len = res.length();
        res = res.substring(0, 2) + (char)(len / 255) + (char)(len % 255) + res.substring(4);
        return res;
    }
    
    private void do_getDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET DATE ")) {
            return;
        }
        job = job.trim();
        final java.lang.String date = job.substring(9, 13);
        final java.lang.String Year = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(0, 4);
        final java.lang.String DateStart = new DT8.DT8_Util().DT8_GetSJMfromDate(Year + "-" + date.substring(0, 2) + "-" + date.substring(2, 4) + " 00:00:00");
        final java.lang.String DateEnd = new DT8.DT8_Util().DT8_GetSJMfromDate(Year + "-" + date.substring(0, 2) + "-" + date.substring(2, 4) + " 23:59:59");
        final java.lang.String cmdString = this.MakeCommand(this.WYM_17, 5, 161, 1, 1, 5, DateStart + DateEnd);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET DATE ")) {
            return;
        }
        job = job.trim();
        java.lang.String setDateCMD = "";
        setDateCMD = job.substring(9);
        setDateCMD = new DT8.DT8_Util().MakeSetDateCommoand(setDateCMD);
        final java.lang.String cmdString = this.MakeCommand(this.WYM_17, 33, 5, 1, 1, 5, setDateCMD);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_getMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String date = job.substring(10, 12);
        final java.lang.String Year = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(0, 4);
        final java.lang.String DateStart = new DT8.DT8_Util().DT8_GetSJMfromDate(Year + "-" + date.substring(0, 2) + "-" + "01 00:00:00");
        int month = java.lang.Integer.valueOf(date);
        ++month;
        if (month == 13) {
            month = 1;
        }
        if (month < 10) {
            date = "0" + java.lang.String.valueOf(month);
        }
        else {
            date = java.lang.String.valueOf(month);
        }
        final java.lang.String DateEnd = new DT8.DT8_Util().DT8_GetSJMfromDate(Year + "-" + date.substring(0, 2) + "-" + "00 23:59:59");
        final java.lang.String cmdString = this.MakeCommand(this.WYM_17, 5, 161, 1, 1, 6, DateStart + DateEnd);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String setMonthCMD = "";
        setMonthCMD = job.substring(10);
        setMonthCMD = new DT8.DT8_Util().MakeSetMonthCommoand(setMonthCMD);
        final java.lang.String cmdString = this.MakeCommand(this.WYM_17, 33, 6, 1, 1, 5, setMonthCMD);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void TCPDataSend(final java.lang.String RemoteIP, final int RemotePort, java.lang.String strSend) {
        strSend = this.Data2MingMa(strSend);
        int idx = -1;
        for (int i = javagprssrv.JGS_Param.TCPServerSockets.size() - 1; i >= 0; --i) {
            if (((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemoteIP.equals(RemoteIP) && ((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemotePort == RemotePort) {
                idx = i;
                break;
            }
        }
        if (idx > -1) {
        	((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(idx)).SendData(strSend);
        }
    }
    
    private java.lang.String Data2MingMa(final java.lang.String strSend) {
        java.lang.String res = "";
        res = strSend;
        res = res.substring(4);
        int idx = -1;
        for (int i = res.length(); i > 0; --i) {
            final int c = res.charAt(i - 1);
            if (c == 128) {
                idx = i - 1;
                break;
            }
        }
        res = res.substring(0, idx);
        return res;
    }
}
